package XComponent.show
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;

	public class XGridBox	extends Sprite
	{

		private var m_entity : DisplayObject;
		
		private var m_mWith : Number = 102;			//默认宽度
		private var m_mHeight : Number = 102;		//默认高度
		
		/**
		 *自动将组建大小设置为实体大小 
		 * @param entity
		 * 
		 */		
		public function XGridBox(entity : DisplayObject = null,width:Number=-1,height:Number=-1)
		{
			if(!entity)
			{
				drawBlueBackGround();
				
				setSize(m_mWith,m_mHeight);
			}
			else
			{
				this.entity = entity;
			}
			
			if(width > 0 && height > 0)
			{
				setSize(width,height);
			}
		}
		
		/**
		 *销毁 
		 * 
		 */		
		public function destory() : void
		{
			m_entity = 				null;
		}
		
		/**
		 *设置宽高 
		 * @param width
		 * @param height
		 * 
		 */		
		public function setSize(width : Number,height : Number) : void
		{
			this.width = 	width;
			this.height = 	height;
		}
		
		/**
		 *绘制蓝色背景 
		 * 
		 */		
		public function drawBlueBackGround() : void
		{
			this.graphics.beginFill(0x0000FF);
			this.graphics.drawRect(0,0,m_mWith,m_mHeight);
			this.graphics.endFill();
		}
		
		/**
		 * 设置实体
		 * @param entity
		 * 
		 */		
		public function set entity(entity : DisplayObject) : void
		{
			clear();
			
			m_entity = entity;
			
			if(entity)
			{
				this.addChild(entity);
			}
		}
		
		/**
		 * 
		 * @return	获取实体 
		 * 
		 */		
		public function get entity() : *
		{
			return m_entity;
		}
		
		public function invalidate() : void
		{
			if(m_entity)
			{
				drawBlueBackGround();
			}
		}
		
		/**
		 *清理 
		 * 
		 */		
		private function clear() : void
		{
			this.graphics.clear();
			
			if(this.numChildren > 0)
			for(var i:int=this.numChildren-1;i>=0;i--)
			{
				this.removeChildAt(i);
			}
		}
	}
}